interface ScreenMenu {
    [propName:string]: any
}
import menus from '@/api/menus'
import { defineStore } from 'pinia'
import type { RouteRecordRaw } from 'vue-router'
const modules = import.meta.glob('@/views/*/*.vue')
const menuList = defineStore('menu', {
    state: function () {
        return {
            allowMenu: [] as RouteRecordRaw []
        }
    },
    actions: {
        generateRoutes() {
            return new Promise((resolve, reject) => {
                let menuData = menus // 后期使用API请求代替
                let menuItems = [] as RouteRecordRaw []
                menuData.forEach((menu) => {
                    let menuItem = {} as RouteRecordRaw
                    menuItem.path = menu.path
                    menuItem.name = menu.name
                    menuItem.meta = menu.meta
                    if (menu.redirect) menuItem.redirect = menu.redirect
                    menuItem.component = menu.component === 'Layout' ? import('@/layout/index.vue') : modules[`/src/${menu.component}.vue`]
                    menuItem.children = [] as RouteRecordRaw []
                    menu.children.forEach((item: ScreenMenu, k: number) => {
                        menuItem.children![k] = {} as RouteRecordRaw
                        menuItem.children![k].path = item.path
                        menuItem.children![k].name = item.name
                        menuItem.children![k].meta = item.meta
                        menuItem.children![k].component = modules[`/src/${item.component}.vue`]
                    })
                    menuItems.push(menuItem)
                })
                this.allowMenu = menuItems
                resolve(menuItems)
            })
        }
    }
})

export default menuList


